//
//  CompereDetailsViewController.m
//  RainbowFM
//
//  Created by Kratos on 14/12/11.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "CompereDetailsViewController.h"
#import "LeaveWordsViewController.h"
#import "PhotosViewController.h"

#import "CompereDetailsHeadView.h"
#import "CompereShowView.h"

#import "CompereModel.h"
#import "LoginModel.h"

#import "UserModel.h"

#import "FLViewController.h"
#import "UtilsCell.h"

@interface CompereDetailsViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    NSMutableArray *mediaArray;
}

@property (nonatomic , strong) IBOutlet CompereDetailsHeadView *headView;
@property (nonatomic , strong) CompereShowView *showView;
@property (nonatomic , strong) CompereItem *compereItem;



@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UIButton *followButton;
@end

@implementation CompereDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect headFrame = self.headView.frame;
    headFrame.size.height = 450;
    self.headView.frame = headFrame;
    
    mediaArray = [NSMutableArray array];

}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self update];
}

//更新数据
-(void)update{
    typeof(self) self_ = self;
    [[CompereModel sharedModel] fetchCompereInfoWithAnchorId:_anchorid result:^(CompereItem *compere, NSString *msg) {
        self_.compereItem = compere;
        [self_ updateCompereInfo:self_.compereItem];
        [self initMediaArray];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)initMediaArray{
    [mediaArray removeAllObjects];
    NSArray *picArray = [_compereItem.pics componentsSeparatedByString:@","];
    for (NSString *picURLStr in picArray) {
        [mediaArray addObject:@{@"type" : @"image",@"URL" : picURLStr}];
    }
    
    for (int i=0; i<[_compereItem.videoPicsArray count]; i++) {
        [mediaArray addObject:@{@"type" : @"video",@"picURL" : _compereItem.videoPicsArray[i],@"videoURL" : _compereItem.videosArray[i]}];
    }
    
    [self.collectionView reloadData];
    
}

-(void)updateCompereInfo:(CompereItem *)item
{
    if (item) {
        [_headView updateCompereData:item];
    }else{
        [_headView updateCompereData:nil];
    }

    
    
    if ([item.isNotice intValue] == 1) {
        [self.followButton setImage:[UIImage imageNamed:@"comp_detail_favor_1"] forState:UIControlStateNormal];
    }else{
        [self.followButton setImage:[UIImage imageNamed:@"comp_detail_favor"] forState:UIControlStateNormal];
    }
}



/**
 *  主持人关注
 *
 *  @param sender <#sender description#>
 */
- (IBAction)followAction:(id)sender {
    if ([UserModel sharedModel].userInfo != nil) {
        if ([self.compereItem.isNotice intValue] == 0) {
            [[CompereModel sharedModel] operCompereWithAnchorId:_compereItem.anchorId operType:OperTypeAdd result:^(BOOL success, NSString *msg) {
                if (success) {
                    [self update];
//                    [self.followButton setImage:[UIImage imageNamed:@"comp_detail_favor_1"] forState:UIControlStateNormal];
                    [SVProgressHUD showSuccessWithStatus:msg];
                }
            }];
        }else{
            [[CompereModel sharedModel] operCompereWithAnchorId:_compereItem.anchorId operType:OperTypeDelete result:^(BOOL success, NSString *msg) {
                if (success) {
                    [self update];
//                    [self.followButton setImage:[UIImage imageNamed:@"comp_detail_favor"] forState:UIControlStateNormal];
                    [SVProgressHUD showSuccessWithStatus:msg];
                }
            }];
        }
        
        
        
    }else{
        UINavigationController *controller = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateInitialViewController];
        [self presentViewController:controller animated:YES completion:nil];
    }
}





-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return mediaArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CompShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CompShowCell" forIndexPath:indexPath];
    NSDictionary *dict = mediaArray[indexPath.row];
    [cell showType:dict[@"type"]];
    if ([dict[@"type"] isEqualToString:@"image"]) {
        [cell.cellImgeView sd_setImageWithURL:[NSURL URLWithString:dict[@"URL"]]];
    }
    else{
        
        [cell.cellImgeView sd_setImageWithURL:[NSURL URLWithString:dict[@"picURL"]]];
    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = mediaArray[indexPath.row];
    if ([dict[@"type"] isEqualToString:@"image"]) {
        PhotosViewController *photosViewController = [PhotosViewController new];
        [photosViewController setImageUrls:@[dict[@"URL"]]];
        [self.navigationController pushViewController:photosViewController animated:YES];
    }
    
}

#pragma mark - Navigation

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    if ([identifier isEqualToString:@"leavewords"]){
        if ([LoginModel sharedModel].userID == nil) {
            UINavigationController *controller = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateInitialViewController];
            [self presentViewController:controller animated:YES completion:nil];
            return NO;
        }
    }
    
    return YES;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"message"]) {
        [[segue destinationViewController] setCompereItem:_compereItem];
    }
    else if ([segue.identifier isEqualToString:@"leavewords"]){
        LeaveWordsViewController *leaveWordsViewController = [segue destinationViewController];
        [leaveWordsViewController setPart:InterlocutionPartOfCompere];
        [leaveWordsViewController setCompereitem:_compereItem];
        leaveWordsViewController.cmtTarget = _compereItem.anchorId;
    }
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
